package com.mszlu.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mszlu.blog.dao.mapper.HobbyMapper;
import com.mszlu.blog.dao.pojo.Hobby;
import com.mszlu.blog.dao.pojo.SysUser;
import com.mszlu.blog.service.HobbyService;
import com.mszlu.blog.utils.EmptyUtils;
import com.mszlu.blog.utils.JWTUtils;
import com.mszlu.blog.vo.ErrorCode;
import com.mszlu.blog.vo.Result;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @Package: com.mszlu.blog.service.impl
 * @ClassName: HobbyServiceImpl
 * @Author: 陈威
 * @CreateTime: 2021/12/28 22:30
 * @Description:
 */
@SuppressWarnings("all")
@Service
@Transactional
public class HobbyServiceImpl implements HobbyService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private HobbyMapper mapper;

    @Override
    public Result setHobby(Hobby hobby, String token) {
        if(EmptyUtils.isEmpty(hobby)){
            return new Result(999, ErrorCode.PARAM_NULL.getMsg());
        }
        if(EmptyUtils.isEmpty(token)){
            return new Result(999, ErrorCode.TOKEN_NULL.getMsg());
        }
        redisTemplate.opsForValue().get("TOKEN_" + token);
        Map<String, Object> stringObjectMap = JWTUtils.checkToken(token);
        if (stringObjectMap == null) {
            return null;
        }
        String userJson = redisTemplate.opsForValue().get("TOKEN_" + token);
        if (StringUtils.isBlank(userJson)) {
            return null;
        }
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
        LambdaQueryWrapper<Hobby> queryWrapper = new LambdaQueryWrapper<Hobby>();
        queryWrapper.eq(Hobby::getUid, sysUser.getId());
        Hobby selectOne = mapper.selectOne(queryWrapper);
        int update;
        if (selectOne != null) {
            LambdaUpdateWrapper<Hobby> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Hobby::getUid, sysUser.getId());
            update = mapper.update(hobby, updateWrapper);
        } else {
            hobby.setUid(sysUser.getId());
            update = mapper.insert(hobby);
        }

        return Result.success(update > 0 ? "保存成功" : "保存失败");
    }

    @Override
    public Result getHobby(String token) {
        if(EmptyUtils.isEmpty(token)){
            return new Result(999, ErrorCode.TOKEN_NULL.getMsg());
        }
        redisTemplate.opsForValue().get("TOKEN_" + token);
        Map<String, Object> stringObjectMap = JWTUtils.checkToken(token);
        if (stringObjectMap == null) {
            return null;
        }
        String userJson = redisTemplate.opsForValue().get("TOKEN_" + token);
        if (StringUtils.isBlank(userJson)) {
            return null;
        }
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
        LambdaQueryWrapper<Hobby> queryWrapper = new LambdaQueryWrapper<Hobby>();
        queryWrapper.eq(Hobby::getUid, sysUser.getId());
        Hobby selectOne = mapper.selectOne(queryWrapper);
        if (selectOne == null) {
            selectOne.setCatoon("暂未设置,请尽快设置~~");
            selectOne.setGame("暂未设置,请尽快设置~~");
            selectOne.setGirl("暂未设置,请尽快设置~~");
            selectOne.setColor("暂未设置,请尽快设置~~");
            selectOne.setName("暂未设置,请尽快设置~~");
        }
        return Result.success(selectOne);
    }
}